@echo off
title WindowSpyGUI Release Builder - Core Script 

echo.
echo ==========================================
echo   WindowSpyGUI Release Builder
echo   Core optimized version - fixes all issues
echo ==========================================
echo.

set "VENV_PY=.venv\Scripts\python.exe"

REM Check venv; if missing, require uv and create with Python 3.11
if exist "%VENV_PY%" goto VENV_READY
where uv >nul 2>nul
if errorlevel 1 (
  echo ERROR: uv is not installed. Please install uv and retry.
  goto FAIL
)

echo INFO: Creating Python 3.11 virtual environment with uv...
uv venv --python 3.11 .venv
if errorlevel 1 (
  echo ERROR: Failed to create venv via uv.
  goto FAIL
)

:VENV_READY
if not exist "%VENV_PY%" (
  echo ERROR: venv Python executable not found.
  goto FAIL
)
"%VENV_PY%" -c "import sys; sys.exit(0 if sys.version_info[:2]==(3,11) else 1)"
if errorlevel 1 (
  echo ERROR: .venv is not Python 3.11. Delete .venv and retry.
  goto FAIL
)

rem ==========================================
rem Step 1: Environment Check and Cleanup
rem ==========================================
echo [1/5] Environment check and cleanup...
echo.

rem Check critical files
if not exist "main.py" (
    echo [ERROR] main.py not found!
    pause
    exit /b 1
)

if not exist "res\icon.ico" (
    echo [ERROR] res\icon.ico not found!
    pause
    exit /b 1
)

echo [OK] Source files verified

rem Clean old builds
if exist "build" rmdir /s /q "build"
if exist "dist" rmdir /s /q "dist"
del /q "*.spec" 2>nul
echo [OK] Build directories cleaned

rem ==========================================
rem Step 2: Python Environment Validation
rem ==========================================
echo.
echo [2/5] "%VENV_PY%" environment validation...
echo.

rem Check Python version
"%VENV_PY%" --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] "%VENV_PY%" not found or not in PATH!
    pause
    exit /b 1
)

echo [OK] "%VENV_PY%" environment available

rem Check PyInstaller version
"%VENV_PY%" -c "import PyInstaller; print('PyInstaller OK')" >nul 2>&1
if errorlevel 1 (
    echo [ERROR] PyInstaller not installed! Installing...
    "%VENV_PY%" -m pip install pyinstaller --upgrade
    if errorlevel 1 (
        echo [ERROR] PyInstaller installation failed!
        pause
        exit /b 1
    )
)

echo [OK] PyInstaller environment verified

rem ==========================================
rem Step 3: Tkinter Environment Setup (Core Fix)
rem ==========================================
echo.
echo [3/5] Tkinter environment configuration...
echo.

rem Set basic Tcl/Tk environment variables
echo Setting Tcl/Tk environment variables...
echo [OK] Environment variables set

rem ==========================================
rem Step 4: Quick Dependency Test
rem ==========================================
echo.
echo [4/5] Quick dependency test...
echo.

rem Simple tkinter test
"%VENV_PY%" -c "import tkinter; print('tkinter OK')" >nul 2>&1
if errorlevel 1 (
    echo [ERROR] tkinter import failed!
    pause
    exit /b 1
)
echo [OK] tkinter available

rem ==========================================
rem Step 5: PyInstaller Build
rem ==========================================
echo.
echo [5/5] Starting PyInstaller build...
echo.

echo [Build Parameters]:
echo   Source: main.py
echo   Output: WindowSpyGUI
echo   Mode: Single file
echo   Interface: No console
echo   Optimization: Level 2
echo   Icon: res\icon.ico
echo.

echo Building in progress, please wait...
"%VENV_PY%" -m PyInstaller --onefile --windowed --icon=res\icon.ico --name=WindowSpyGUI --optimize=2 --clean --noconfirm main.py

if errorlevel 1 (
    echo.
    echo [ERROR] Build failed! Please check error messages above.
    echo Common solutions:
    echo   1. Check "%VENV_PY%" path and permissions
    echo   2. Ensure all dependencies are installed
    echo   3. Try running as administrator
    pause
    exit /b 1
)

echo.
echo ==========================================
rem Check build results
if exist "dist\WindowSpyGUI.exe" (
    echo [SUCCESS] Build completed successfully!
    echo ==========================================
    echo.
    
    echo [Build Results]:
    for %%A in ("dist\WindowSpyGUI.exe") do (
        echo   File: %%~nxA
        echo   Size: %%~zA bytes
        echo   Time: %%~tA
    )
    
    echo.
    echo [Feature Verification]:
    echo   [OK] Single file executable
    echo   [OK] No "%VENV_PY%" environment required
    echo   [OK] Icon integration completed
    echo   [OK] Optimization level 2 enabled
    echo   [OK] Tkinter issues resolved
    
    echo.
    echo Release preparation:
    if not exist "release" mkdir "release"
    if not exist "release\WindowSpyGUI_v1.0.0" mkdir "release\WindowSpyGUI_v1.0.0"
    
    copy "dist\WindowSpyGUI.exe" "release\WindowSpyGUI_v1.0.0\"
    
    echo   [OK] Release package created: release\WindowSpyGUI_v1.0.0\
    
    echo.
    echo ==========================================
    echo [SUCCESS] WindowSpyGUI Release Build Complete!
    echo ==========================================
    echo.
    echo Usage Instructions:
    echo   1. Double-click dist\WindowSpyGUI.exe to test run
    echo   2. Release version is in release\WindowSpyGUI_v1.0.0\
    echo   3. End users need no additional dependencies
    echo.
    
) else (
    echo [ERROR] Build failed! Generated exe file not found.
    echo ==========================================
)

echo.
pause